package com.ruoyi.faceplus.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.faceplus.constant.FacePlusConstants;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.faceplus.domain.FaceConfig;
import com.ruoyi.faceplus.service.IFaceConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人脸库配置Controller
 *
 * @author ruoyi
 * @date 2025-01-18
 */
@RestController
@RequestMapping("/faceplus/faceconfig")
public class FaceConfigController extends BaseController {
    @Autowired
    private IFaceConfigService faceConfigService;

    @Autowired
    @Qualifier("redisTemplate")
    public RedisTemplate redisTemplate;

    /**
     * 查询人脸库配置列表
     */
    @PreAuthorize("@ss.hasPermi('faceplus:faceconfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(FaceConfig faceConfig) {
        startPage();
        List<FaceConfig> list = faceConfigService.selectFaceConfigList(faceConfig);
        return getDataTable(list);
    }

    /**
     * 导出人脸库配置列表
     */
    @PreAuthorize("@ss.hasPermi('faceplus:faceconfig:export')")
    @Log(title = "人脸库配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaceConfig faceConfig) {
        List<FaceConfig> list = faceConfigService.selectFaceConfigList(faceConfig);
        ExcelUtil<FaceConfig> util = new ExcelUtil<FaceConfig>(FaceConfig.class);
        util.exportExcel(response, list, "人脸库配置数据");
    }

    /**
     * 获取人脸库配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('faceplus:faceconfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(faceConfigService.selectFaceConfigById(id));
    }

    /**
     * 新增人脸库配置
     */
    @PreAuthorize("@ss.hasPermi('faceplus:faceconfig:add')")
    @Log(title = "人脸库配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FaceConfig faceConfig) {
        return toAjax(faceConfigService.insertFaceConfig(faceConfig));
    }

    /**
     * 修改人脸库配置
     */
    @PreAuthorize("@ss.hasPermi('faceplus:faceconfig:edit')")
    @Log(title = "人脸库配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FaceConfig faceConfig) {
        try {
            int result = faceConfigService.updateFaceConfig(faceConfig);
            // 写入缓存
            // 创建 ObjectMapper 对象
            ObjectMapper objectMapper = new ObjectMapper();
            // 步骤 2：将实体对象转换为 JSON 字符串
            String jsonString = objectMapper.writeValueAsString(faceConfig);
            // 写入缓存
            redisTemplate.opsForValue().set(FacePlusConstants.FACE_CONFIG, jsonString);

            return toAjax(result);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return toAjax(0);
        }
    }

    /**
     * 删除人脸库配置
     */
    @PreAuthorize("@ss.hasPermi('faceplus:faceconfig:remove')")
    @Log(title = "人脸库配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(faceConfigService.deleteFaceConfigByIds(ids));
    }
}
